<?php

namespace app\api\controller;

use think\Request;
use think\Response;
use think\facade\Db;


class Seat extends Wap
{


    /**
     * 场地选择
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            $data = array();
            $list = Db::name('customer_method')->where('store_id', $post['store_id'])->select();
            foreach ($list as $i => $v) {
                $data['method'][] = array(
                    'name' => $v['name'],
                    'id' => $v['id'],
                );
            }
            $roomlist = Db::name('mini_room_list')->where('id', $post['store_id'])->find();
            if ($roomlist['province_code']) {
                if ($roomlist['province_code']) {
                    $roomlist['province_name'] = Db::name('city')->where('citycode', $roomlist['province_code'])->find();
                }
                if ($roomlist['city_code']) {
                    $roomlist['city_name'] = Db::name('city')->where('citycode', $roomlist['city_code'])->find();
                }
                if ($roomlist['district_code'] > 0) {
                    $roomlist['district_name'] = Db::name('city')->where('citycode', $roomlist['district_code'])->find();
                } else {
                    $roomlist['district_name']['name'] = '';
                }
                $roomlist['address_lan'] = $roomlist['province_name']['name'] . $roomlist['city_name']['name'] . $roomlist['district_name']['name'] . $roomlist['address'];

            }

            $info = Db::name('coutomer_make')->where('store_id', '=', $post['store_id'])->find();
            $data['room'] = array(
                'address' => $roomlist['address_lan'],
                'content' => isset($info['content']) ? $info['content'] : '',
            );
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }


    /**
     * 座位预定
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function method(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            $list = Db::name('customer_seat')->where('store_id', $post['store_id'])->where('method_id', $post['method_id'])->where('mark', 1)->select();
            $data = array();
            foreach ($list as $i => $v) {

                $info = Db::name('mini_happy')->where('id', $v['happy_id'])->find();
                $data[] = array(
                    'name' => $info['name'],
                    'id' => $v['id'],
                    'happy_id' => $info['id'],
                );

            }
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }


    /**
     * 玩法人数
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function lineup(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            $list = Db::name('customer_seat_order')->where('status', 'in', [1, 2])->where('method_id', $post['method_id'])->where('seat_id', $post['seat_id'])->where('happy_id', $post['happy_id'])->count();
            $data['num'] = $list;
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }


    /**
     * 下单
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */


    public function Lottery(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            $post['user_id'] = $user['id'];

            $hours_time = $this->time();
            $hours_add_time = $hours_time['hours_add_time'];
            $hours_end_time = $hours_time['hours_end_time'];
            $time_add = $hours_time['time_add'];
            // 查询房间
            $room = Db::name('customer_house')->where('store_id', $post['store_id'])->where('mark', 1)->select();
            $orderList = Db::name('customer_seat_order')->where('seat_id', $post['seat_id'])->where('happy_id', $post['happy_id'])->where('method_id', $post['method_id'])->where('status', 'in', [0, 1, 2])->count();
            //查询活动
            $course = Db::name('customer_seat')->where('id', $post['seat_id'])->find();
            $info = Db::name('mini_happy')->where('id', $course['happy_id'])->find();
            $course['num'] = $info['num'];
            $seat = Db::name('customer_method')->where('id', $post['method_id'])->find();
            for ($i = 1; $i <= $course['num']; $i++) {
                $k[] = $i;
            }


            $order = Db::name('customer_seat_order')->where('status', 'in', [0, 1, 2])->where('store_id', $post['store_id'])->group('house_id')->select();


            $info = Db::name('coutomer_make')->where('store_id', '=', $post['store_id'])->find();
            $room_list_id = array();
            if (count($order) > 0) {
                foreach ($order as $i => $v) {
                    $room_list_id[] = $v['house_id'];
                }
            }
            $room_id = array();


            if ($orderList >= $course['num']) {
                if ($orderList % $course['num'] == 0) {

                    // 随机房间号
                    foreach ($room as $i => $v) {
                        if (in_array($v['id'], $room_list_id)) {
                            unset($v[$i]);
                        } else {
                            $room_id[] = $v['id'];
                        }
                    }
                    if (count($room_id) < 1) {
                        $arr['code'] = 101;
                        $arr['message'] = '当前场次房间已满,请更改场次';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                }
            } else {


                if ($orderList > $course['num'] && $orderList == 0) {
                    // 随机房间号
                    foreach ($room as $i => $v) {
                        if (in_array($v['id'], $room_list_id)) {
                            unset($v[$i]);
                        } else {
                            $room_id[] = $v['id'];
                        }
                    }
                    if (count($room_id) < 0) {
                        $arr['code'] = 101;

                        $arr['message'] = '当前场次房间已满,请更改场次';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                }
            }


            $data = array(
                'order_on' => $this->orderOn(),
                'user_id' => $user['id'],
                'seat_id' => $post['seat_id'],
                'store_id' => $post['store_id'],
                'happy_id' => $post['happy_id'],
                'method_id' => $post['method_id'],
                'create_time' => time(),
                'price' => $info['price'],
                'add_time' => strtotime($post['add_time'] . '' . $hours_add_time[$seat['add_time'] + 1] . ':' . $time_add[$seat['add_hour']]),
                'end_time' => strtotime($post['add_time'] . '' . $hours_end_time[$seat['end_time'] + 1] . ':' . $time_add[$seat['end_hour']]),
            );
            if ($data['end_time'] < time()) {
                $arr['code'] = 101;

                $arr['message'] = '本场次已结束';
                Response::create($arr, 'json')->send();
            }

            if (Db::name('customer_seat_order')->insert($data)) {


                $da['order_on'] = $data['order_on'];
                $arr['code'] = 200;
                $arr['data'] = $da;
                $arr['message'] = '获取成功';
                Response::create($arr, 'json')->send();
                exit();
            }


        }

    }

    /**
     * 处理数据
     * @param $arr
     * @return bool|string
     */
    public function arr_to_str($arr)
    {
        $t = '';
        foreach ($arr as $v) {
            $temp[] = $v;
        }
        foreach ($temp as $v) {
            $t .= $v . ',';
        }
        $t = substr($t, 0, -1); // 利用字符串截取函数消除最后一个逗号
        return $t;
    }

    /**
     * 删除订单
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            if (Db::name('customer_seat_order')->where('user_id', $user['id'])->where('status', 0)->delete()) {
                $order = Db::name('customer_seat_order')->where('user_id', $user['id'])->where('status', 0)->find();
                Db::name('customer_order_cleaning')->where('order_on', $order['order_on'])->delete();
                $arr['code'] = 200;
                $arr['message'] = '删除成功';
                Response::create($arr, 'json')->send();
                exit();
            }
            $arr['code'] = 200;
            $arr['message'] = '删除成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }

    /**
     * 签到
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function sing(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            $lat = $post['lat'];
            $lng = $post['lag'];
            // 计算距离

            $order = Db::name('customer_seat_order')->where('order_on', $post['order_on'])->find();

            if ($order['sign'] > 0) {
                $arr['code'] = 101;
                $arr['message'] = '已签到，不可重复签到';
                Response::create($arr, 'json')->send();
                exit();
            }
            if ($order['end_time'] < time()) {
                $arr['code'] = 101;
                $arr['message'] = '订单已结束，不可签到';
                Response::create($arr, 'json')->send();
                exit();
            }
            $room = Db::name('mini_room_list')->where('id', $order['store_id'])->find();

            $info = Db::name('coutomer_make')->where(['store_id' => $order['store_id']])->find();
            if ($info['door_distance'] > 0) {
                // 计算 距离
                if ($lat > 0) {
                    $distance = Wap::distance($lat, $lng, $room['latitude'], $room['longitude']);
                    if ($distance * 1000 > $info['door_distance']) {
                        $arr['code'] = 101;
                        $arr['message'] = '不在签到范围内';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                }
            }
            $sing = array(
                'sign' => 1,
                'sign_time' => time(),
            );
            if (Db::name('customer_seat_order')->where('order_on', $post['order_on'])->update($sing)) {
                $arr['code'] = 200;
                $arr['message'] = '签到成功';
                Response::create($arr, 'json')->send();
                exit();
            }
        }
    }


    /**
     * 详情
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            $order = Db::name('customer_seat_order')->where('user_id', $user['id'])->where('status', 'in', [1, 2])->find();

            $data = array();
            if ($order) {
                $roomlist = Db::name('mini_room_list')->where('id', $order['store_id'])->find();
                if ($roomlist['province_code']) {
                    if ($roomlist['province_code']) {
                        $roomlist['province_name'] = Db::name('city')->where('citycode', $roomlist['province_code'])->find();
                    }
                    if ($roomlist['city_code']) {
                        $roomlist['city_name'] = Db::name('city')->where('citycode', $roomlist['city_code'])->find();
                    }
                    if ($roomlist['district_code'] > 0) {
                        $roomlist['district_name'] = Db::name('city')->where('citycode', $roomlist['district_code'])->find();
                    } else {
                        $roomlist['district_name']['name'] = '';
                    }
                    $roomlist['address_lan'] = $roomlist['province_name']['name'] . $roomlist['city_name']['name'] . $roomlist['district_name']['name'] . $roomlist['address'];

                }
                $door_list = array();
                if ($order['house_id'] > 0) {
                    $house_info = Db::name('customer_house')->where('id', $order['house_id'])->find();
                    if ($house_info['door_id']) {
                        $door = Db::name('customer_door_list')->where('id', $house_info['door_id'])->find();
                        if ($door['pid'] > 0) {
                            $doord = Db::name('customer_door_list')->where('id', $door['pid'])->find();
                            $door_list[] = array(
                                'door_id' => $doord['door_id'],
                                'door_name' => $doord['name'],
                            );
                        }
                        $door_list[] = array(
                            'door_id' => $door['door_id'],
                            'door_name' => $door['name'],
                        );
                    }
                    switch ($order['lottery']) {
                        case 1:
                            $num_name = "东位";
                            break;
                        case 2:
                            $num_name = "西位";
                            break;
                        case 3:
                            $num_name = "南位";
                            break;
                        case 4:
                            $num_name = "北位";
                            break;
                    }
                    $order['lottery'] = $house_info['name'] . '-' . $num_name;
                }
                $method = Db::name('customer_method')->where('id', $order['method_id'])->find();
                $info = Db::name('mini_happy')->where('id', $order['happy_id'])->find();
                $list = Db::name('customer_seat_order')->where('status', 'in', [1, 2])->where('method_id', $order['method_id'])->where('seat_id', $order['seat_id'])->where('happy_id', $order['happy_id'])->count();
                $store = Db::name('mini_room_list')->where('id', $order['store_id'])->find();
                if ($store['province_code']) {
                    if ($store['province_code']) {
                        $store['province_name'] = Db::name('city')->where('citycode', $store['province_code'])->find();
                    }
                    if ($store['city_code']) {
                        $store['city_name'] = Db::name('city')->where('citycode', $store['city_code'])->find();
                    }
                    if ($store['district_code'] > 0) {
                        $store['district_name'] = Db::name('city')->where('citycode', $store['district_code'])->find();
                        if ($store['district_name']) {
                            if ($store['district_name'] == "") {
                                $store['district_name']['name'] = ' ';
                            }
                        }
                    } else {
                        $store['district_name']['name'] = ' ';
                    }
                    $store['address_lan'] = $store['province_name']['name'] . $store['city_name']['name'] . $store['district_name']['name'] . $store['address'];
                }
                $data = array(
                    'create_time' => date('Y(年)-m(月)-d(日)', $order['add_time']),
                    'name' => $method['name'],
                    'happy_name' => $info['name'],
                    'address' => $roomlist['address_lan'],
                    'num' => $list,
                    'status' => $order['status'],
                    'is_lottery' => $order['lottery'],
                    'sign' => $order['sign'],
                    'order_on' => $order['order_on'],
                    'door_list' => $door_list,
                    'lat' => $store['latitude'],
                    'lng' => $store['longitude'],
                    'store_id' => $store['id'],

                );
            }
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }

}